package com.vitas.coin.ui.fm

import android.content.res.ColorStateList
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.media3.common.Player
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.snap.base.util.BaseUtil
import com.snap.common.main.BaseMVVMFg
import com.vitas.coin.R
import com.vitas.coin.data.event.PausePlayEvent
import com.vitas.coin.data.event.StartPlayEvent
import com.vitas.coin.databinding.FrgCateBinding
import com.vitas.coin.manager.PlayManager
import com.vitas.coin.util.AdUtil
import com.vitas.coin.vm.CateVM


class CateFrg : BaseMVVMFg<FrgCateBinding, CateVM>(), View.OnClickListener {
    private val activeColor = Color.parseColor("#000000")
    private val normalColor = Color.parseColor("#969696")
    private var lastSelectIndex = 0

    companion object {
        const val TAG_TEXT_TAB = "TAB"
    }


    override fun createVM(): CateVM {
        return CateVM().apply {
            actionAct = { requireActivity() }
        }
    }

    override fun doDataBind() {
        binding.viewModel = viewModel
    }

    override fun getContentViewId(): Int {
        return R.layout.frg_cate
    }

    override fun onViewCreated() {
        initEvent()
        initBottomPlay()
        initObserve()
        setViewPager()
        viewModel.loadData()
    }

    private fun initObserve() {
        BaseUtil.observer(this, StartPlayEvent::class.java) {
            if (binding.clBottom.visibility == View.GONE) {
                binding.clBottom.visibility = View.VISIBLE
            }
            viewModel.musicName.value = it.musicObj.musicName
            binding.ivPlay.setImageResource(R.mipmap.icon_pause)
        }

        BaseUtil.observer(this, PausePlayEvent::class.java) {
            binding.ivPlay.setImageResource(R.mipmap.icon_start)
        }

    }

    private fun initBottomPlay() {
        val playStatus = PlayManager.getInstance().playStatus
        if (playStatus == Player.STATE_IDLE) {
            binding.clBottom.visibility = View.GONE
        } else {
            binding.clBottom.visibility = View.VISIBLE
            viewModel.musicName.value = PlayManager.getInstance().musicName
            if (PlayManager.getInstance().isPlaying) {
                binding.ivPlay.setImageResource(R.mipmap.icon_pause)
            } else {
                binding.ivPlay.setImageResource(R.mipmap.icon_start)
            }
        }
    }


    private fun setViewPager() {
        // 预加载
        binding.viewPager.offscreenPageLimit = 2
        // Adapter
        binding.viewPager.adapter = viewModel.mViewPageAdapter
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                AdUtil.adInsert(requireActivity())
//                if (binding.tabLayout.tabCount <= 0) {
//                    return
//                }
//                val tv = binding.tabLayout.getTabAt(position)?.customView as TextView
//                tv.textSize = 15f
//
//                val tvLast = binding.tabLayout.getTabAt(lastSelectIndex)?.customView as TextView
//                tvLast.textSize = 15f
//                lastSelectIndex = position
            }
        })


        val mediator = TabLayoutMediator(
            binding.tabLayout, binding.viewPager
        ) { tab, position -> //这里可以自定义TabView
            val tabView = TextView(requireContext())
            tabView.gravity = Gravity.CENTER
            val states = arrayOfNulls<IntArray>(2)
            states[0] = intArrayOf(android.R.attr.state_selected)
            states[1] = intArrayOf()
            val colors = intArrayOf(activeColor, normalColor)
            val colorStateList = ColorStateList(states, colors)
            tabView.text = viewModel.musicCateList[position].name
            tabView.textSize = 15f
            tabView.tag = TAG_TEXT_TAB
            tabView.setTextColor(colorStateList)
            tab.customView = tabView
        }
        mediator.attach()
    }


    private fun initEvent() {
        binding.ivShoucangList.setOnClickListener(this)
    }


    override fun onClick(p0: View?) {
        context?.let {
            PlayManager.getInstance().showList(requireActivity())
        }
    }
}